home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / exif.lib / dev / examples / showthumbnail / main.c next >
Encoding:
C/C++ Source or Header  |  2001-01-03  |  4.7 KB  |  199 lines

  1. /*    This example use of exif.library reads and displays any exif
  2.         thumbnail (in RGB format).
  3. */
  4.  
  5. #include <stdio.h>
  6.  
  7. #include <exec/memory.h>
  8. #include <exec/types.h>
  9.  
  10. #include <clib/exec_protos.h>
  11. #include <clib/cybergraphics_protos.h>
  12. #include <clib/intuition_protos.h>
  13.  
  14. #include <pragmas/exec_pragmas.h>
  15. #include <pragmas/intuition_pragmas.h>
  16. #include <pragmas/cybergraphics_pragmas.h>
  17.  
  18. #include <guigfx/guigfx.h>
  19. #include <pragmas/guigfx_pragmas.h>
  20. #include <guigfx/guigfx_protos.h>
  21.  
  22. #include <jpeg/jpeg.h>
  23. #include <jpeg/jpeg_protos.h>
  24. #include <jpeg/jpeg_pragmas.h>
  25.  
  26. #include <exif/exif.h>
  27. #include <exif/exif_protos.h>
  28. #include <exif/exif_pragmas.h>
  29.  
  30. /* Function prototypes */
  31. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y );
  32.  
  33. struct Library *ExifBase, *JpegBase;
  34.  
  35. char *version = "$VER: showthumbnail 1.0 (3.1.2001)";
  36.  
  37. void main( int argc, char **argv )
  38. {
  39.     if ( argv[1] )
  40.     {
  41.         ExifBase = OpenLibrary( "exif.library", 1 );
  42.         JpegBase = OpenLibrary( "jpeg.library", 6 );
  43.  
  44.         if ( ExifBase && JpegBase )
  45.         {
  46.             struct Exif *exif;
  47.  
  48.             exif = ReadExif( argv[1], NULL );
  49.             if ( exif )
  50.             {
  51.                 ULONG err;
  52.                 struct JPEGDecHandle *jph;
  53.                 UBYTE *buffer;
  54.                 ULONG x, y, rs;
  55.                 UBYTE colourspace;
  56.                 ULONG bpp;
  57.  
  58.                 printf( "Exif length=%ld\n", exif->ExifLength );
  59.  
  60.                 if ( exif->ThumbnailData )
  61.                 {
  62.                     printf( "Thumbnail size=%ld\n", exif->ThumbnailSize );
  63.  
  64.                     err = AllocJPEGDecompress( &jph,
  65.                         JPG_SrcMemStream, exif->ThumbnailData,
  66.                         JPG_SrcMemStreamSize, exif->ThumbnailSize,
  67.                         TAG_DONE );
  68.                     if ( !err )
  69.                     {
  70.                         err = GetJPEGInfo( jph,
  71.                             JPG_Width, &x, JPG_Height, &y,
  72.                             JPG_ColourSpace, &colourspace,
  73.                             JPG_BytesPerPixel, &bpp,
  74.                             JPG_RowSize, &rs,
  75.                             TAG_DONE );
  76.                         if ( !err )
  77.                         {
  78.                             buffer = AllocBufferFromJPEG( jph, TAG_DONE );
  79.                             if ( buffer != NULL )
  80.                             {
  81.                                 err = DecompressJPEG( jph,
  82.                                     JPG_DestRGBBuffer, buffer,
  83.                                     TAG_DONE );
  84.                                 if ( !err )
  85.                                 {
  86.                                     DisplayGuiGfx( buffer, colourspace, rs, x, y );
  87.                                 }
  88.                                 else printf( "decompress jpeg error:%d\n", err );
  89.  
  90.                                 FreeJPEGBuffer( buffer );
  91.                             }
  92.                             else printf( "cant allocate rgb buffer\n" );
  93.                         }
  94.                         else printf( "get jpeg info error:%d\n", err );
  95.  
  96.                         FreeJPEGDecompress( jph );
  97.                     }
  98.                     else printf("cant allocate jpeg decompress = %ld\n",err);
  99.                 }
  100.                 else printf( "thumbnail not in jpeg format (or no thumbnail)\n" );
  101.  
  102.                 FreeExif( exif );
  103.             }
  104.         }
  105.  
  106.         if ( ExifBase != NULL ) CloseLibrary( ExifBase );
  107.         else printf( "you need exif.library v1.0 minimum\n" );
  108.  
  109.         if ( JpegBase != NULL ) CloseLibrary( JpegBase );
  110.         else printf( "you need jpeg.library v6.0 minimum\n" );
  111.     }
  112.     else
  113.     {
  114.         printf( "no source file specified\n" );
  115.     }
  116. }
  117.  
  118. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y )
  119. {
  120.     struct Library *GuiGFXBase, *IntuitionBase;
  121.  
  122.     IntuitionBase = OpenLibrary( "intuition.library", 39 );
  123.     GuiGFXBase = OpenLibrary( "guigfx.library", 17 );
  124.     if ( GuiGFXBase && IntuitionBase )
  125.     {
  126.         struct Window *win;
  127.         APTR dh, pi;
  128.         UBYTE *argb;
  129.  
  130.         win = OpenWindowTags( NULL,
  131.             WA_Title, "Proof",
  132.             WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  133.                 WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  134.                 WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  135.             WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  136.                 IDCMP_SIZEVERIFY | IDCMP_NEWSIZE | IDCMP_RAWKEY,
  137.             WA_Left, 16,
  138.             WA_Top, 16,
  139.             WA_Width, x,
  140.             WA_Height, y,
  141.             TAG_DONE );
  142.  
  143.         if ( win != NULL )
  144.         {
  145.             dh = ObtainDrawHandle( NULL, win->RPort, win->WScreen->ViewPort.ColorMap, TAG_DONE );
  146.  
  147.             if ( dh )
  148.             {
  149.                 argb = AllocVec( x * y * 4, MEMF_PUBLIC | MEMF_CLEAR );
  150.  
  151.                 if ( argb )
  152.                 {
  153.                     int i, count = 0;
  154.                     char **dest;
  155.  
  156.                     /* Convert an RGB buffer to an ARGB buffer setting A to 0.*/
  157.                     dest = (char **)argb;
  158.  
  159.                     for ( i = 0; i < x * y * 3; i += 3 )
  160.                     {
  161.                         dest[count++] = (char *)( ( (ULONG) * (char**)&buffer[i] ) >> 8 );
  162.                     }
  163.  
  164.                     pi = MakePicture( argb, x, y, GGFX_PixelFormat, PIXFMT_0RGB_32, TAG_DONE );
  165.  
  166.                     if ( pi )
  167.                     {
  168.                         struct Message *msg;
  169.  
  170.                         DrawPicture( dh, pi, 0, 0, NULL );
  171.  
  172.                         Wait( 1L << win->UserPort->mp_SigBit );
  173.  
  174.                         while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  175.  
  176.                         DeletePicture( pi );
  177.                     }
  178.                     else printf( "failed to create picture\n" );
  179.  
  180.                     FreeVec( argb );
  181.                 }
  182.                 else printf( "failed to allocate argb buffer\n" );
  183.  
  184.                 ReleaseDrawHandle( dh );
  185.             }
  186.             else printf( "failed to get drawhandle\n" );
  187.  
  188.             CloseWindow( win );
  189.         }
  190.         else printf( "failed to open window\n" );
  191.     }
  192.     else printf( "failed to open guigfx.library\n" );
  193.  
  194.     if ( IntuitionBase ) CloseLibrary( IntuitionBase );
  195.  
  196.     if ( GuiGFXBase ) CloseLibrary( GuiGFXBase );
  197.     else printf( "you need guigfx.library v17.0 minimum\n" );
  198. }
  199.